<?php

namespace plugin\webman\houwang;
use support\Db;
use GatewayWorker\Lib\Gateway;
use support\Redis;
use plugin\webman\common\Common;
class Method
{

    // 获取当前猴王信息
    static function getmokeyinfo(){
        $houwang_expect = Db::table("wa_houwang_expect")->where("status",1)->orderByDesc("id",true)->limit(1)->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        if(!$houwang_expect){
            $houwang = Db::table("wa_houwang")->where('status',1)->orderByRaw("id",true)->limit(1)->get()->map(function ($value) {return (array)$value;})
            ->toArray();
            $inexpect =Db::table("wa_houwang_expect")->insert([
                'expect'=>1,  //第几轮
                'houwang_id'=>$houwang[0]['id'],
                'houwang_no'=>$houwang[0]['number'],
                'status'=>1,
                'lefttime'=>'10800',
            ]);
            $houwang_expect = Db::table("wa_houwang_expect")->where("status",1)->orderByDesc("id",true)->limit(1)->get()->map(function ($value) {return (array)$value;})
            ->toArray();
        }
        $houinfo = Db::table("wa_houwang")->where("id",$houwang_expect[0]['houwang_id'])->first();
        // 总被攻击的血量
        $bloodaall = (string)self::getbloodall($houwang_expect[0]['houwang_id'],$houwang_expect[0]['expect']);
        $houblood = (string)$houinfo->blood;
        $leftblood = (string)$houblood - $bloodaall;
        $url =Common::getsiteurl('siteurl');
        $lastzhainfo = Db::table("wa_houwang_record")->orderByDesc("id",true)->limit(1)->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        if($lastzhainfo){
            $last = Common::getuserinfo($lastzhainfo[0]['uid']);
        }else{
            $last = [];
        }
        $info = [
            'expect'=>$houwang_expect[0]['expect'],
            'houwang_id'=>$houwang_expect[0]['houwang_id'],
            'houwang_no'=>$houwang_expect[0]['houwang_no'],
            'name'=>'第'.$houwang_expect[0]['houwang_no'].'号猴王',
            'blood'=>$houblood,
            'jiangchi'=>self::convertToTenThousand($houinfo->start_jiangchi),
            'lastreward'=>self::convertToTenThousand($houinfo->start_jiangchi * 0.01),
            'lastreward2'=>$houinfo->start_jiangchi * 0.01,
            'blood_beizha'=>$bloodaall,
            'blood_left'=>(string)$leftblood,
            'lefttime'=>self::getSeconds($houwang_expect[0]['lefttime']),
            'status'=>$houinfo->status,
            // 'backimg'=>$url.$houinfo->backimg,
            // 'mokeyimg'=>$url.$houinfo->mokey,
            // 'mokeyavatar'=>$url.$houinfo->avatar,
            'lastzhainfo'=> $last ,
            'backimg'=>$houinfo->backimg,
            'mokeyimg'=>$houinfo->mokey,
            'mokeyavatar'=>$houinfo->avatar,         
        ];
        return $info;
    }

    // 获取倒计时
    static function getdaojishi(){
        $houwang_expect = Db::table("wa_houwang_expect")->where("status",1)->orderByDesc("id",true)->limit(1)->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        // 逃跑倒计时减去1
        $houwang_expect[0]['lefttime'] = $houwang_expect[0]['lefttime'] - 1;
        $uptime = Db::table("wa_houwang_expect")->where("id",$houwang_expect[0]['id'])->update(['lefttime'=>$houwang_expect[0]['lefttime']]);

        return $houwang_expect[0]['lefttime'];
        
    }



    // 以万为单位换算
    static function convertToTenThousand($num) {

        // 检查数字是否为整数
        // if (!is_int($num)) {
        //   return false;
        // }
    //    var_dump($num);die;
        // 小于10000的数字直接返回
        if ($num < 10000) {
          return $num;
        }
       
        // 大于或等于10000的数字进行转换
        $result = $num / 10000;
        return round($result, 2) . '万';
      
      }
    
    //   用ID获取猴王被炸了多少血量

    static function getbloodall($houwang_id,$expect){
        $all = Db::table("wa_houwang_record")->where(['houwang_id'=>$houwang_id,'expect'=>$expect])->sum('blood');
        return $all;
    }

    // 时间换算成小时
    static function getSeconds($cha,$type = true){
       if($cha <60){
           $seconds = $cha;
           if($type){
               $seconds = $seconds < 10 ? '0'.$seconds : $seconds;
           }
           return $seconds.'秒';
       }else if( $cha < 3600 ){
           $minutes = floor($cha/60);
           $seconds = $cha - ($minutes*60);
           if($type){
               if($minutes<10){
                   $minutes='0'.$minutes;
               }
               if($seconds<10){
                   $seconds='0'.$seconds;
               }
           }
           return $minutes.'分'.$seconds.'秒';
       }else if($cha<86400){
           $hour = floor($cha/3600);
           $minutes_cha = $cha - ($hour*3600);
           $minutes = floor($minutes_cha/60);
           $seconds = $minutes_cha - ($minutes*60);
           if($type){
               if($hour<10){
                   $hour='0'.$hour;
               }
               if($minutes<10){
                   $minutes='0'.$minutes;
               }
               if($seconds<10){
                   $seconds='0'.$seconds;
               }
               return $hour.'小时'.$minutes.'分钟'.$seconds.'秒';
           }
       }else{
           $day = floor($cha/86400);
           $hour_cha = $cha - $day*86400;
           $hour = floor($hour_cha/3600);
           $minutes_cha = $hour_cha - $hour*3600;
           $minutes = floor($minutes_cha/60);
           $seconds = $minutes_cha - ($minutes*60);
           if($type){
               if($day<10){
                   $day='0'.$day;
               }
               if($hour<10){
                   $hour='0'.$hour;
               }
               if($minutes<10){
                   $minutes='0'.$minutes;
               }
               if($seconds<10){
                   $seconds='0'.$seconds;
               }
           }
           return $day.'天'.$hour.':'.$minutes.':'.$seconds;
       }
   }


    //获取炸血规则
    static function getrules(){
        $rules = Db::table("wa_houwang_rule")->where("status",1)->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        return $rules;
    }    

    static function getruleByid($id){
        $rule = Db::table("wa_houwang_rule")->where(["status"=>1,'id'=>$id])->first();
        return $rule;
    }
    
    // 扣除猴王血量
    static function upmokeyblood($houwang_id,$blood){
        // $houwang = Db::table("wa_houwang")->where(["id"=>$houwang_id])->first();
        // $houwang_blood = $houwang->blood;
        // $houwang_blood = $houwang_blood - $blood;
        // if($houwang_blood<0){
        //     $houwang_blood = 0;
        // }
        // $up = Db::table("wa_houwang")->where(["id"=>$houwang_id])->update(["blood"=>$houwang_blood]);
        // if($up){
        //     return true;
        // }else{
        //     return false;
        // }
    }

    //发送广播 血量更新
    

    // 增加猴王逃跑时间
    static function addmokeytime($expect,$houwang_id){
        $key = "zhahouwang".$expect."_".$houwang_id;
        $time = Redis::get($key);
        if($time){
            if($time<10){
                $time = intval($time) + 1;
                Redis::set($key,$time);
            }else{
                Redis::del($key);
                // 给猴王逃跑时间增加1秒
                $houwang_expect = Db::table("wa_houwang_expect")->where(['expect'=>$expect,'houwang_id'=>$houwang_id])->first();
                if($houwang_expect->lefttime < 10800){
                    $newtime = $houwang_expect->lefttime + 1;
                    Db::table("wa_houwang_expect")->where(['expect'=>$expect,'houwang_id'=>$houwang_id])->update(['lefttime'=>$newtime]);
                }
            }
        }else{
            Redis::set($key,1);
        }
        
    }
}